﻿Imports ADOEntity
Imports System.Data
Partial Class Administrator_SPurgeJob
    Inherits Web.UI.BasePage
    Private Const strMenuCode As String = "M61"

    Private Enum enmStatus_Purge
        process = 0
        comple = 1
        Problem_Query = 2
        Problem_CreateXML = 3
    End Enum

    Dim PurgeEntity As New PurgeEntities
    Dim dsPurge As New DataSet
    Dim clsQuery As clsDatabase
    Dim clsDataTable As New clsLINQToDataTable
    Dim clsSessionMenuRight As clsSessionMenuRight
    Dim convertDate As clsDateFormat

#Region " Even Page "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        chkIsLogin()
        'clsSessionMenuRight = Session("ClsMenuRight")

        btnManual.Attributes.Add("onclick", "return confirm('" + "ต้องการลบ Log File ใช้หรือไม่" + "');")
       If (Not IsPostBack) Then
            IsDataBind()
            CreateControlMP()
        Else
            dsPurge = Session("dsTblMaster")
            grdPurge.DataSource = dsPurge.Tables("sPurge")
            grdPurge.DataBind()
        End If

    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("dsTblMaster") = dsPurge
    End Sub

#End Region

#Region "SetForm"

    Sub CreateControlMP()

        Dim lblForm As Label = CType(Master.FindControl("lblForm"), Label)
        lblForm.Text = "ประวัติการใช้งาน > ประวัติการล้างข้อมูล"
    End Sub

#End Region

#Region " Even Button "

    Protected Sub btnManual_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnManual.Click
        Try
            RunBatchFile()
            ' System.Threading.Thread.Sleep(5000)
            IsDataBind()
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                                         Me.GetType(), _
                                                                         "clientScript", _
                                                                         "alert('" + ex.Message + "')", _
                                                                         True)
        End Try
    End Sub

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim param As String = "{ 'rptType' : '1'}"
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", "OpenWindowWithPost('../Form/frmRptMaster.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)
    End Sub

#End Region

#Region "Even GridView"

    Protected Sub grdPurge_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdPurge.RowDataBound
        Try
            If (e.Row.RowType.ToString = "DataRow") Then
                Dim lblUpdateDate As Label = CType(e.Row.FindControl("lblUpdateDate"), Label)
                If Not (lblUpdateDate.Text = "") Then
                    convertDate = New clsDateFormat
                    lblUpdateDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, lblUpdateDate.Text)
                    convertDate = Nothing
                End If

                Dim lblStatus As Label = CType(e.Row.FindControl("lblStatus"), Label)
                If Not (lblStatus.Text = "") Then
                    If (enmStatus_Purge.comple = lblStatus.Text.Trim) Then
                        lblStatus.Text = "การดำเนินาการเสร็จสิ้น"
                    ElseIf (enmStatus_Purge.process = lblStatus.Text.Trim) Then
                        lblStatus.Text = "กำลังดำเนินการ"
                    ElseIf (enmStatus_Purge.Problem_Query = lblStatus.Text.Trim) Then
                        lblStatus.Text = "เกิดข้อผิดพลาดในการดำเนินการ ล้างข้อมูล"
                    ElseIf (enmStatus_Purge.Problem_CreateXML = lblStatus.Text.Trim) Then
                        lblStatus.Text = "เกิดข้อผิดพลาดในการดำเนินการ สร้าง XML"
                    End If

                End If

            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub grdPurge_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdPurge.SelectedIndexChanged

    End Sub

    Protected Sub grdPurge_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdPurge.PageIndexChanging
        grdPurge.PageIndex = e.NewPageIndex
        clearGvIndex()
        DataBind()
    End Sub

#End Region

#Region " Function"

    Private Sub clearGvIndex()
        grdPurge.SelectedIndex = -1

    End Sub

    Private Sub IsDataBind()
        If dsPurge.Tables.Contains("sPurge") Then
            dsPurge.Tables.Clear()
        End If

        Dim qryPurge = From p In PurgeEntity.SPURGEJOB _
                         Select p.PURGE_DESC, p.LAST_PURGE_DATE, p.STATUS_PURGE

        dsPurge.Tables.Add(clsDataTable.LINQToDataTable(qryPurge, "sPurge").Copy)

        grdPurge.DataSource = dsPurge.Tables("sPurge")
        grdPurge.DataBind()

        clsQuery = Nothing
    End Sub
    Private pathDriveOnServer As String = "D:\\PurgeDATA\\"
    Private Const PathFileFloderBatch As String = "D:\\PurgeDATA\\PurgeCommand.bat"
    'Private Function RunBatchFile(ByVal strPathBatch As String) As Boolean

    Private Function RunBatchFile() As Boolean
        Dim blnComple As Boolean = False
        Try
            Dim psi As New System.Diagnostics.ProcessStartInfo("cmd.exe")
            psi.UseShellExecute = False
            psi.RedirectStandardInput = True
            psi.RedirectStandardOutput = True
            psi.RedirectStandardError = True
            psi.WorkingDirectory = pathDriveOnServer
            'DECLARE PROCESS
            Dim proc As System.Diagnostics.Process
            proc = System.Diagnostics.Process.Start(psi)
            'PATH OPENED AS TEXT BY STREAM READER
            Dim strm As System.IO.StreamReader
            strm = System.IO.File.OpenText(PathFileFloderBatch)
            ''OUTPUT 
            Dim sout As System.IO.StreamReader
            sout = proc.StandardOutput
            ''INPUT
            Dim sin As System.IO.StreamWriter
            sin = proc.StandardInput
            ''COMMAND TO CMD
            While (strm.Peek() <> -1)
                sin.WriteLine(strm.ReadLine())
            End While
            strm.Close()
            Dim stEchoFmt As String
            stEchoFmt = "# {0} run successfully. Exiting"
            sin.WriteLine(String.Format(stEchoFmt, PathFileFloderBatch))
            System.Threading.Thread.Sleep(5000)
            sin.WriteLine("EXIT")
            proc.Close()

            ''READ THE EXECUTED BATCH FILE

            'Dim results As String
            'results = sout.ReadToEnd.Trim

            ''CLOSE IN/OUT

            sin.Close()
            sout.Close()

            'sout.Close()
            blnComple = True
        Catch ex As Exception
            Throw New Exception("เกิดข้อผิดพลาด RunBatchFile ")
            blnComple = False
        End Try
        Return blnComple
    End Function

#End Region


    Protected Sub btnRefresh_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        Try
            ' RunBatchFile()
            ' System.Threading.Thread.Sleep(5000)
            IsDataBind()
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                                         Me.GetType(), _
                                                                         "clientScript", _
                                                                         "alert('" + ex.Message + "')", _
                                                                         True)
        End Try
    End Sub
End Class
